package cn.iocoder.yudao.module.hs.service.picktaskmaterial;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterial.vo.PickTaskMaterialPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterial.vo.PickTaskMaterialSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.picktaskmaterial.PickTaskMaterialDO;
import cn.iocoder.yudao.module.hs.dal.mysql.picktask.PickTaskMapper;
import cn.iocoder.yudao.module.hs.dal.mysql.picktaskmaterial.PickTaskMaterialMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.PICK_TASK_MATERIAL_NOT_EXISTS;

/**
 * 拣货任务机型明细 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class PickTaskMaterialServiceImpl implements PickTaskMaterialService {

    @Resource
    private PickTaskMaterialMapper pickTaskMaterialMapper;

    @Resource
    private PickTaskMapper pickTaskMapper;

    @Override
    public Long createPickTaskMaterial(PickTaskMaterialSaveReqVO createReqVO) {
        // 插入
        PickTaskMaterialDO pickTaskMaterial = BeanUtils.toBean(createReqVO, PickTaskMaterialDO.class);
        pickTaskMaterialMapper.insert(pickTaskMaterial);
        // 返回
        return pickTaskMaterial.getId();
    }

    @Override
    public void updatePickTaskMaterial(PickTaskMaterialSaveReqVO updateReqVO) {
        // 校验存在
        validatePickTaskMaterialExists(updateReqVO.getId());
        // 更新
        PickTaskMaterialDO updateObj = BeanUtils.toBean(updateReqVO, PickTaskMaterialDO.class);
        pickTaskMaterialMapper.updateById(updateObj);
    }

    @Override
    public void deletePickTaskMaterial(Long shipOrderId) {
        // 删除
        pickTaskMaterialMapper.delete(PickTaskMaterialDO::getShipOrderId, shipOrderId);
    }

    private void validatePickTaskMaterialExists(Long id) {
        if (pickTaskMaterialMapper.selectById(id) == null) {
            throw exception(PICK_TASK_MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public PickTaskMaterialDO getPickTaskMaterial(Long id) {
        return pickTaskMaterialMapper.selectById(id);
    }

    @Override
    public PageResult<PickTaskMaterialDO> getPickTaskMaterialPage(PickTaskMaterialPageReqVO pageReqVO) {
        return pickTaskMaterialMapper.selectPage(pageReqVO);
    }

    @Override
    public void insertBatch(List<PickTaskMaterialDO> list) {
        pickTaskMaterialMapper.insertBatch(list);
    }

    @Override
    public List<PickTaskMaterialDO> getPickTaskMaterialList(Long pickTaskId) {
        return pickTaskMaterialMapper.selectList(PickTaskMaterialDO::getPickTaskId, pickTaskId);
    }

    @Override
    public PickTaskMaterialDO getPickTaskMaterial(Long pickTaskId, String materialModeCode) {
        LambdaQueryWrapperX<PickTaskMaterialDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(PickTaskMaterialDO::getPickTaskId, pickTaskId)
                .eq(PickTaskMaterialDO::getMaterialModeCode, materialModeCode);
        return pickTaskMaterialMapper.selectOne(queryWrapperX);
    }
}